//
// RS Game Framework
// Copyright © 2009 Jedd Haberstro
// jhaberstro@gmail.com
// 
// $Id:
//

#ifndef RS_PORTABILITY_PLATFORMS_POSIX_IMPLEMENTATIONS_HPP
#define RS_PORTABILITY_PLATFORMS_POSIX_IMPLEMENTATIONS_HPP

#include "rs/ReferenceCounted.hpp"
#include <pthread.h>

#if RS_PLATFORM == RS_PLATFORM_MACOSX
#   include <mach/semaphore.h>
#   include <mach/task.h>
#   include <mach/mach_init.h>
#else
#   include <semaphore.h>
#endif

namespace rs
{
    namespace internal
    {
        struct ConditionImplementation : public ReferenceCounted
        {
            pthread_cond_t conditionHandle_;
        };
        
        struct MutexImplementation : public ReferenceCounted
        {
            pthread_mutex_t mutexHandle_;
        };
        
        struct SemaphoreImplementation : public ReferenceCounted
        {
#       if RS_PLATFORM == RS_PLATFORM_MACOSX
            semaphore_t semaphoreHandle_;
#       else
            sem_t semaphoreHandle_;
#       endif
        };
        
        struct ThreadIDImplementation
        {
            ThreadIDImplementation() {
            }
               
            ThreadIDImplementation(pthread_t id)
            : id_(id) {
            }
            
            ThreadIDImplementation(ThreadIDImplementation const& other)
            : id_(other.id_) {
            }
            
            ThreadIDImplementation& operator=(ThreadIDImplementation const& other) {
                if (this != &other) {
                    id_ = other.id_;
                }
                
                return *this;
            }

            Boolean operator==(ThreadIDImplementation const& other) const {
                return pthread_equal(id_, other.id_) != 0;
            }

            Boolean operator!=(ThreadIDImplementation const& other) const {
                return pthread_equal(id_, other.id_) == 0;
            }

            pthread_t id_;
        };

        struct ThreadImplementation : public ReferenceCounted
        {
            ThreadImplementation() {
            }

            pthread_t threadId_;
        };
    }
}

#endif // RS_PORTABILITY_PLATFORMS_POSIX_IMPLEMENTATIONS_HPP
